package org.ccay.excel.exporter.util.impl;

import java.util.Map;

import javax.inject.Inject;
import javax.inject.Named;

import org.ccay.async.task.ExportTaskVO;
import org.ccay.async.task.ImportTaskVO;
import org.ccay.async.task.service.IExportTaskService;
import org.ccay.async.task.service.IImportTaskService;
import org.ccay.core.exception.ApplicationException;
import org.ccay.core.io.FileStoreType;
import org.ccay.core.io.VFSFileObject;
import org.ccay.core.request.IUserPrincipal;
import org.ccay.core.request.impl.RequestContext;
import org.ccay.core.util.StringUtil;
import org.ccay.download.DownloadException;
import org.ccay.download.IDownloadSupport;
import org.ccay.vfs.ApacheVFSFileObject;

/**
 * Excel附件下载servlet
 */
@Named("IDownloadSupport.Excel")
public class ExcelDownloadSupport implements IDownloadSupport {

	// 判断附件下载的类型导入或导出
	private static final String EXCEL_EXPORT = "EXP";

	private static final String EXCEL_IMPORT = "IMP"; 

	@Inject
	private IImportTaskService excelImportTaskService;

	@Inject
	private IExportTaskService excelExportTaskService;
 
	
	/**
	 * 获得下载类型
	 * 
	 * @author chaowangbang
	 * @since 2012-2-13
	 * @return
	 */
	public String getDownloadType() {
		return "Excel";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ccay.download.support.IDownloadSupport#getFileInfo(java.util.Map)
	 */
	public VFSFileObject getFileInfo(Map<String, String> map)
			throws ApplicationException {
		// excelID
		int id = Integer.valueOf(map.get("excelId"));
		// 获取任务ID
		String execFlag = map.get("execFlag");
		// 判断是否有错误信息
		String remark = map.get("remark"); 
		
		if (EXCEL_IMPORT.equals(execFlag)) {
			return getImportFileObject(id, remark);
		} else if (EXCEL_EXPORT.equals(execFlag)) {
			return getExportFileObject(id);
		}
		return null;
	}
	
	protected VFSFileObject getImportFileObject(int id, String remark) throws ApplicationException {
		ImportTaskVO excelImportTaskVO = excelImportTaskService.findImportTaskById(id);
		String displayName = excelImportTaskVO.getDisplayFileName();
		//下载路径
		String reletivePath = null;
		if ("true".equalsIgnoreCase(remark)) {//下载错误提示文件
			reletivePath = excelImportTaskVO.getRemark();
			//转换错误提示文件名称
			String baseName = displayName.replace(".xlsx", "");
			if(reletivePath.endsWith(".err")){
				displayName = baseName + ".err.xlsx";
			}
			if(reletivePath.endsWith(".txt")){
				displayName = baseName + ".err.txt";
			}
		}else {
			reletivePath = excelImportTaskVO.getFilePath();
		}
		if(StringUtil.isNullOrEmpty(reletivePath)){
			throw new DownloadException("ccay.download.support.00010002");
		}
		ApacheVFSFileObject fileObject =  new ApacheVFSFileObject("excel",reletivePath,FileStoreType.ARCHIVE);
		fileObject.setDisplayName(displayName);
		return fileObject;
	}

	protected VFSFileObject getExportFileObject(int id) throws ApplicationException{
		ExportTaskVO excelExportTaskVO = excelExportTaskService.findExportTaskById(id);
		String reletivePath = excelExportTaskVO.getFileName();
		ApacheVFSFileObject fileObject =  new ApacheVFSFileObject("excel",reletivePath,FileStoreType.ARCHIVE);
		//设置下载文件名为数据库存储的文件名，以便下载时用户看到的文件名与查询到的一致
		fileObject.setDisplayName(excelExportTaskVO.getTmpName()+".xlsx");
		return fileObject;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.ccay.download.support.IDownloadSupport#validatePrivilege(java.util.Map)
	 */
	public boolean validatePrivilege(Map<String, String> map)
			throws ApplicationException {
		// 获取当前用户
		IUserPrincipal user = RequestContext.getCurrent().getUser();
		int id = Integer.valueOf(map.get("excelId"));	 

		// 获取任务ID
		String execFlag = map.get("execFlag"); 

		if (EXCEL_IMPORT.equals(execFlag)) {
			// 根据id查询导出任务
			ImportTaskVO excelImportTaskVO = excelImportTaskService.findImportTaskById(id);
			return user.getUserId().equals(excelImportTaskVO.getCreatedBy());
		} else if (EXCEL_EXPORT.equals(execFlag)) {
			// 根据id查询导出任务
			ExportTaskVO excelExportTaskVO = excelExportTaskService.findExportTaskById(id);
			return excelExportTaskVO.getCreatedBy().equals(user.getUserId());
		} else {
			return false;
		}
	}
}